package com.lixun.passwordbook;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.content.Intent;
import ohos.data.DatabaseHelper;
import ohos.data.dataability.DataAbilityUtils;
import ohos.data.rdb.*;
import ohos.data.resultset.ResultSet;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;
import ohos.utils.PacMap;
import java.io.FileDescriptor;

public class DataAbility extends Ability {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "DemoData");

    private static final String DB_NAME = "dataability.db";

    private static final String DB_TAB_NAME = "pwb";

    private static final String DB_COLUMN_ID = "id";

    private static final String DB_COLUMN_NICKNAME = "nickname";

    private static final String DB_COLUMN_USERNAME = "username";

    private static final String DB_COLUMN_PASSWOED = "password";

    private static final String DB_COLUMN_TYPE = "type";

    private static final String DB_COLUMN_MODIFYTIME = "modifytime";

    private static final String DB_COLUMN_EXPLAIN = "explain";


    private static final int DB_VERSION = 1;

    private StoreConfig config = StoreConfig.newDefaultConfig(DB_NAME);

    private RdbStore rdbStore;

    private RdbOpenCallback rdbOpenCallback = new RdbOpenCallback() {
        @Override
        public void onCreate(RdbStore store) {
            store.executeSql("create table if not exists "
                    + DB_TAB_NAME + " ("
                    + DB_COLUMN_ID + " integer primary key autoincrement, "
                    + DB_COLUMN_NICKNAME + " text not null, "
                    + DB_COLUMN_USERNAME + " text, "
                    + DB_COLUMN_PASSWOED + " text, "
                    + DB_COLUMN_TYPE + " text, "
                    + DB_COLUMN_EXPLAIN + " text, "
                    + DB_COLUMN_MODIFYTIME + " datetime)");
            HiLog.info(LABEL_LOG,"创建数据库");
            store.executeSql("insert into pwb(nickname,username) values('游戏','aaa')");
            store.executeSql("insert into pwb(nickname,username,type,explain,modifytime) values('测试','aaa','默认','邮箱：131212@qq.com','2021-09-25 12:34:56')");
            store.executeSql("insert into pwb(nickname,username,type,modifytime) values('测试2','aaa','默认','2021-09-30 12:34:56')");
            store.executeSql("insert into pwb(nickname,type,modifytime) values('测3','默认','2021-09-30 12:34:58')");
            HiLog.info(LABEL_LOG,"添加数据");
        }

        @Override
        public void onUpgrade(RdbStore store, int oldVersion, int newVersion) {
        }
    };

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        HiLog.info(LABEL_LOG, "DataAbility onStart");
        DatabaseHelper databaseHelper = new DatabaseHelper(this);
        rdbStore = databaseHelper.getRdbStore(config, DB_VERSION, rdbOpenCallback, null);
    }

    @Override
    public ResultSet query(Uri uri, String[] columns, DataAbilityPredicates predicates) {
        RdbPredicates rdbPredicates = DataAbilityUtils.createRdbPredicates(predicates, DB_TAB_NAME);
        ResultSet resultSet = rdbStore.query(rdbPredicates, columns);
        if (resultSet == null) {
            HiLog.info(LABEL_LOG, "resultSet is null");
        }
        return resultSet;
    }

    @Override
    public int insert(Uri uri, ValuesBucket value) {
        HiLog.info(LABEL_LOG, "DataAbility insert");
        String path = uri.getLastPath();
        if (!"pwb".equals(path)) {
            HiLog.info(LABEL_LOG, "DataAbility insert path is not matched");
            return -1;
        }
        ValuesBucket values = new ValuesBucket();
        values.putString(DB_COLUMN_NICKNAME, value.getString(DB_COLUMN_NICKNAME));
        values.putString(DB_COLUMN_USERNAME, value.getString(DB_COLUMN_USERNAME));
        values.putString(DB_COLUMN_PASSWOED, value.getString(DB_COLUMN_PASSWOED));
        values.putString(DB_COLUMN_TYPE, value.getString(DB_COLUMN_TYPE));
        values.putString(DB_COLUMN_EXPLAIN, value.getString(DB_COLUMN_EXPLAIN));
        values.putString(DB_COLUMN_MODIFYTIME, value.getString(DB_COLUMN_MODIFYTIME));

        int index = (int) rdbStore.insert(DB_TAB_NAME, values);
        DataAbilityHelper.creator(this, uri).notifyChange(uri);
        return index;
    }

    @Override
    public int delete(Uri uri, DataAbilityPredicates predicates) {
        RdbPredicates rdbPredicates = DataAbilityUtils.createRdbPredicates(predicates, DB_TAB_NAME);
        int index = rdbStore.delete(rdbPredicates);
        HiLog.info(LABEL_LOG, "delete: " + index);
        DataAbilityHelper.creator(this, uri).notifyChange(uri);
        return index;
    }

    @Override
    public int update(Uri uri, ValuesBucket value, DataAbilityPredicates predicates) {
        RdbPredicates rdbPredicates = DataAbilityUtils.createRdbPredicates(predicates, DB_TAB_NAME);
        int index = rdbStore.update(value, rdbPredicates);
        HiLog.info(LABEL_LOG, "update: " + index);
        DataAbilityHelper.creator(this, uri).notifyChange(uri);
        return index;
    }

    @Override
    public FileDescriptor openFile(Uri uri, String mode) {
        return null;
    }

    @Override
    public String[] getFileTypes(Uri uri, String mimeTypeFilter) {
        return new String[0];
    }

    @Override
    public PacMap call(String method, String arg, PacMap extras) {
        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }
}